package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseTimeDialogFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.DefaultAsyncHttpClient;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.HolyRestDays;
import com.isunland.managesystem.entity.HolyRestDaysOriginal;
import com.isunland.managesystem.entity.RequisitionDetail;
import com.isunland.managesystem.entity.SuccessMessage;
import com.isunland.managesystem.entity.WorkHour;
import com.isunland.managesystem.utils.DateUtil;
import com.isunland.managesystem.utils.FileUtil;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ToastUtil;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Date;
import java.util.HashMap;
import org.apache.http.Header;
import org.json.JSONException;

@SuppressWarnings("deprecation") public class CheckHolyFragment extends CheckDetailFragment {
  protected static final int REQUEST_ACTUAL_DATE_END = 1;
  protected static final int REQUEST_ACTUAL_DATE_START = 2;
  public static final int REQUEST_HOLY_TYPE = 5;
  private static final int CHOOSE_FILE = 8;

  private TextView mTvHolyType;

  private CheckBox mCbIsNotCount;// 不计入次数限定.
  private EditText mEtReason;
  private RadioButton mRbApplyHoly; //申请
  private RadioButton mRbSupplyHoly;//补填
  private String ifLaterWrite = "F";//是否补填,默认为申请，不补填
  private TextView mTvActualStartTime;
  private TextView mTvActualEndTime;
  private EditText mEtActualHours;
  //	private EditText mEtPlanDays;
  private EditText mEtActualDays;
  private TextView mFileCheck;// 附件
  private TextView mTvMark;// 病假提示信息
  private TextView mTvRestDays;// 可用带薪假和可用调休假天数

  /**
   * 开始时间标题
   */
  private TextView mTvBegainTime;
  private TextView mTvEndTime;
  private TextView mTvTimeHours;
  private TextView mTvDays;
  private String mHolyType = "";
  /**
   * 是否不计入次数限定
   */
  private String mIsNotCountType = "F";
  private String mPath;
  /**
   * 服务端返回附件
   */
  private String accessoryName = "";

  private void findViews(View v) {

    mTvHolyType = (TextView) v.findViewById(R.id.tv_holyType_checkHoly);

    mCbIsNotCount = (CheckBox) v.findViewById(R.id.cb_isNotCount_checkHoly);
    mEtReason = (EditText) v.findViewById(R.id.et_overTimeReason_checkOver);
    /**请假时间标题设置*/
    mTvBegainTime = (TextView) v.findViewById(R.id.tv_beginTime);
    mTvEndTime = (TextView) v.findViewById(R.id.tv_endTime);
    mTvTimeHours = (TextView) v.findViewById(R.id.tv_timeHours);
    mTvDays = (TextView) v.findViewById(R.id.tv_days);
    /**请假单申请*/
    RadioGroup mRgHolyType = (RadioGroup) v.findViewById(R.id.rg_type);
    mRbApplyHoly = (RadioButton) v.findViewById(R.id.rb_apply);
    mRbSupplyHoly = (RadioButton) v.findViewById(R.id.rb_supply);
    mRbApplyHoly.setText(R.string.applyholy);
    mRbSupplyHoly.setText(R.string.supplyholy);

    mTvActualStartTime = (TextView) v.findViewById(R.id.tv_actualBeginTime_checkOver);
    mTvActualEndTime = (TextView) v.findViewById(R.id.tv_actualEndTime_checkOver);
    mEtActualHours = (EditText) v.findViewById(R.id.et_actualOverTimeHours_checkOver);

    mEtActualDays = (EditText) v.findViewById(R.id.et_naturalHolyDays_checkHoly);
    LinearLayout mAddFileCheck = (LinearLayout) v.findViewById(R.id.iv_addFile_check);
    mFileCheck = (TextView) v.findViewById(R.id.tv_file_check);
    mTvMark = (TextView) v.findViewById(R.id.tv_mark);
    mTvRestDays = (TextView) v.findViewById(R.id.tv_restDays);

    if (mFileUrl != null) {
      String file = mFileUrl.substring(mFileUrl.lastIndexOf("/") + 1, mFileUrl.length());
      mFileCheck.setText(file);
    }
    LogUtil.i("mFileUrl==" + mFileUrl);

    mRgHolyType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
      @Override public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
          case R.id.rb_apply:
            ifLaterWrite = "F";
            mTvBegainTime.setText(R.string.planBeginTime);
            mTvEndTime.setText(R.string.planEndTime);
            mTvTimeHours.setText(R.string.planHolyHours);
            mTvDays.setText(R.string.planHolyDays);
            mTvActualStartTime.setHint(R.string.planBeginTimeHint);
            mTvActualEndTime.setHint(R.string.planEndTimeHint);
            break;
          case R.id.rb_supply:
            ifLaterWrite = "T";
            break;
        }
        initRadio(ifLaterWrite);
      }
    });
    mTvActualStartTime.setOnClickListener(new OnClickListener() {
      @Override public void onClick(View v) {
        showDialog(mTvActualStartTime.getText().toString().trim(), R.string.chooseTime,
            REQUEST_ACTUAL_DATE_START);
      }
    });
    mTvActualEndTime.setOnClickListener(new OnClickListener() {
      @Override public void onClick(View v) {
        showDialog(mTvActualEndTime.getText().toString().trim(), R.string.chooseTime,
            REQUEST_ACTUAL_DATE_END);
      }
    });

    mTvHolyType.setOnClickListener(new OnClickListener() {

      @Override public void onClick(View v) {
        showDialogHolyType();
      }
    });
    mCbIsNotCount.setOnCheckedChangeListener(new OnCheckedChangeListener() {

      @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
          mIsNotCountType = "T";
        } else {
          mIsNotCountType = "F";
        }
      }
    });

    mAddFileCheck.setOnClickListener(new OnClickListener() {

      @Override public void onClick(View v) {
        chooseFileDialog();
      }
    });

    //		if (mMenuType == EDIT) {
    mFileCheck.setOnClickListener(new OnClickListener() {

      @Override public void onClick(View v) {
        if (!TextUtils.isEmpty(mFileUrl)) {

          ExtraDownLoadDialogFragment dialog =
              ExtraDownLoadDialogFragment.newInstance("", mFileUrl);
          FragmentManager fm = getActivity().getSupportFragmentManager();
          dialog.show(fm, "");
        }
      }
    });
    //		}

    if (mMenuType == WAIT_CHECK || mMenuType == CHECK_PASS) {
      mTvHolyType.setEnabled(false);
      mCbIsNotCount.setEnabled(false);
      mEtReason.setEnabled(false);
      mRbApplyHoly.setEnabled(false);
      mRbSupplyHoly.setEnabled(false);

      mTvActualStartTime.setEnabled(false);
      mTvActualEndTime.setEnabled(false);
      mEtActualHours.setEnabled(false);
      mEtActualDays.setEnabled(false);
      mAddFileCheck.setEnabled(false);
    }
    if (mMenuType == TODO) {
      //待办流程确认时间,只可编辑时间
      mTvHolyType.setEnabled(false);
      mCbIsNotCount.setEnabled(false);
      mEtReason.setEnabled(false);
      mRbApplyHoly.setEnabled(false);
      mRbSupplyHoly.setEnabled(false);
      mAddFileCheck.setEnabled(false);
    }
  }

  private void initRadio(String ifLaterWrite) {
    if ("F".equalsIgnoreCase(ifLaterWrite)) {
      mTvBegainTime.setText(R.string.planBeginTime);
      mTvEndTime.setText(R.string.planEndTime);
      mTvTimeHours.setText(R.string.planHolyHours);
      mTvDays.setText(R.string.planHolyDays);
      mTvActualStartTime.setHint(R.string.planBeginTimeHint);
      mTvActualEndTime.setHint(R.string.planEndTimeHint);
    }
    if ("T".equalsIgnoreCase(ifLaterWrite)) {
      mTvBegainTime.setText(R.string.actualBeginTime);
      mTvEndTime.setText(R.string.actualEndTime);
      mTvTimeHours.setText(R.string.actualHolyHours);
      mTvDays.setText(R.string.actualHolyDays);
      mTvActualStartTime.setHint(R.string.actualBeginTimeHint);
      mTvActualEndTime.setHint(R.string.actualEndTimeHint);
    }
  }

  /**
   * 选择附件
   */
  private void chooseFileDialog() {
    DialogFragment dialog = new CompanyForumTopicFileDialogFragment();
    FragmentManager fm = getActivity().getSupportFragmentManager();
    dialog.setTargetFragment(CheckHolyFragment.this, CHOOSE_FILE);
    dialog.show(fm, "");
  }

  /**
   * 选择请假类型
   */
  public void showDialogHolyType() {

    FragmentManager fm = getActivity().getSupportFragmentManager();
    HolyTypeDialogFragment dialog = new HolyTypeDialogFragment();
    // CustomerDialogFragment dialog = CustomerDialogFragment
    // .newInstance(type);
    dialog.setTargetFragment(CheckHolyFragment.this, REQUEST_HOLY_TYPE);
    dialog.show(fm, "");
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    setTitleCustom(R.string.requisitionholy);// 1.更改标题.
    View view = inflater.inflate(R.layout.fragment_check_holy, container, false);
    findViews(view);
    laterViewVisiblity(view, ifAllowSupplement);
    initDetailData();

    return view;
  }

  private void initDetailData() {
    if (mMenuType != ADD) {
      volleypostInit();
    }
  }

  private void volleypostInit() {
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_REQUISITION_DETAIL);
    HashMap<String, String> params = new HashMap<>();
    params.put("attendType", mType);
    params.put("id", mId);
    MyUtils.showDialogLoading(getActivity());
    mActivity.volleyPost(url, params, new VolleyResponse() {

      @Override public void onVolleyResponse(String response) {
        LogUtil.i("initTripResponse=" + response);

        MyUtils.dismissDialog();
        try {
          RequisitionDetail requisitionDetail =
              new Gson().fromJson(response, RequisitionDetail.class);

          accessoryName = requisitionDetail.getAccessoryName();

          ifLaterWrite = requisitionDetail.getIfLaterWrite();
          mTvHolyType.setText(requisitionDetail.getRemark());

          mHolyType = requisitionDetail.getHolidayKindCode();
          mEtReason.setText(requisitionDetail.getOvertimeReason());// 3.获取差旅原因.
          mTvActualStartTime.setText(requisitionDetail.getActSTime());
          mTvActualEndTime.setText(requisitionDetail.getActETime());
          mEtActualHours.setText(MyStringUtil.valueOf(requisitionDetail.getActHolyNum()));
          mEtActualDays.setText(requisitionDetail.getActHolyDays());
          ifLaterWrite = requisitionDetail.getIfLaterWrite();
          /**补填请假单*/
          if ("T".equals(ifLaterWrite)) {
            mRbSupplyHoly.setChecked(true);
          }
          /**申请请假单*/
          else if ("F".equals(ifLaterWrite)) {
            mRbApplyHoly.setChecked(true);
          }
          initRadio(ifLaterWrite);
          // et_naturalTripDays_checkTrip.setText(requisitionDetail
          // .getNaturalDays());// 5.自然天数..

          mTvHolyType.setText(requisitionDetail.getRemark());
          if (SICK.equalsIgnoreCase(requisitionDetail.getHolidayKindCode())) {
            mTvMark.setVisibility(View.VISIBLE);
          }

          mIsNotCountType = requisitionDetail.getIfUserCheck();
          if ("T".equalsIgnoreCase(mIsNotCountType)) {
            // 是否不计入考勤次数选择了是.
            mCbIsNotCount.setChecked(true);
          }
          if ("F".equalsIgnoreCase(mIsNotCountType)) {
            // 选择了否.
            mCbIsNotCount.setChecked(false);
          }
        } catch (Exception e) {
          LogUtil.e("error", e);
          ToastUtil.show(R.string.wrong_data);
        }
      }

      @Override public void onVolleyError(VolleyError arg0) {
        MyUtils.dismissDialog();
        ToastUtil.show(R.string.noConnectionToServer);
      }
    });
  }

  private void showDialog(String startDate, int titleId, int requestCode) {
    Date date = DateUtil.stringToDate(startDate);
    if (null == date) {
      date = new Date();
    }
    FragmentManager fm = getActivity().getSupportFragmentManager();
    DialogFragment dialog = BaseTimeDialogFragment.newInstance(date, titleId);
    dialog.setTargetFragment(CheckHolyFragment.this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) return;
    String startDate;// 开始时间
    String endDate;// 结束时间
    //String currentTime = DateUtil.dateToString(new Date());// 当前时间

    if (requestCode == REQUEST_ACTUAL_DATE_START) {
      Date resultData = (Date) data.getSerializableExtra(BaseTimeDialogFragment.EXTRA_DATE);

      String dateString = DateUtil.dateToString(resultData);

      mTvActualStartTime.setText(dateString);
      startDate = mTvActualStartTime.getText().toString().trim();
      endDate = mTvActualEndTime.getText().toString().trim();
      // 获取工时
      getWorkHours(requestCode, startDate, endDate);
    }
    if (requestCode == REQUEST_ACTUAL_DATE_END) {
      Date resultData = (Date) data.getSerializableExtra(BaseTimeDialogFragment.EXTRA_DATE);

      String dateString = DateUtil.dateToString(resultData);

      mTvActualEndTime.setText(dateString);
      startDate = mTvActualStartTime.getText().toString().trim();
      endDate = mTvActualEndTime.getText().toString().trim();
      // 获取工时
      getWorkHours(requestCode, startDate, endDate);
    }
    if (requestCode == REQUEST_DELETE) {
      volleyPostDelete();
    }
    if (requestCode == REQUEST_SUBMIT) {
      volleyPostSave(TYPE_RUN);
    }

    if (requestCode == REQUEST_HOLY_TYPE && data != null) {
      CustomerDialog customerDialog =
          (CustomerDialog) data.getSerializableExtra(HolyTypeDialogFragment.EXTRA_VALUE);
      mTvHolyType.setText(Html.fromHtml(customerDialog.getName()));
      mHolyType = customerDialog.getId();
      mTvMark.setVisibility(View.GONE);
      //            if (mHolyType != null && !TextUtils.isEmpty(mHolyType)) {
      //                if (mHolyType.equalsIgnoreCase(SICK)) {
      //                    mTvMark.setVisibility(View.VISIBLE);
      //                } else {
      //                    mTvMark.setVisibility(View.GONE);
      //                }
                /*获取可用调休带薪假天数*/
      //                //年假、调休、可用带薪假
      //                if (mHolyType.equalsIgnoreCase(ANNUAL) || mHolyType.equalsIgnoreCase(EXCHANGEHOLIDAY) || mHolyType.equalsIgnoreCase(PAIDLEAVE)) {
      //                    mTvRestDays.setVisibility(View.VISIBLE);
      //                    /* 获取可用调休假和带薪年假 */
      //                    vollePostDays(mHolyType);
      //                } else {
      //                    mTvRestDays.setVisibility(View.GONE);
      //                }
      //            }
    }

    if (requestCode == CHOOSE_FILE && data != null) {
      // 添加附件

      mPath = data.getStringExtra(CompanyForumTopicFileDialogFragment.EXTRA_RESULT);
      if (null != mPath && !TextUtils.isEmpty(mPath)) {
        String mChoseFileName = FileUtil.getFileNameByFilePath(mPath);
        mFileCheck.setText(mChoseFileName);
      }
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  @SuppressWarnings("unused") private void vollePostDays(String holyType) {
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_REST_DAYS);
    mActivity.volleyPost(url, new HashMap<String, String>(), getResponse(holyType));
  }

  private VolleyResponse getResponse(final String holyType) {
    return new VolleyResponse() {
      @Override public void onVolleyError(VolleyError arg0) {
      }

      @Override public void onVolleyResponse(String arg0) throws JSONException {
        HolyRestDaysOriginal original = new Gson().fromJson(arg0, HolyRestDaysOriginal.class);
        HolyRestDays data = original.getData();
        if (null != data && 1 == original.getResult()) {
          if (ANNUAL.equalsIgnoreCase(holyType) || PAIDLEAVE.equalsIgnoreCase(holyType)) {
            mTvRestDays.setText(getString(R.string.wageDays, data.getWageDays()));
          } else if (EXCHANGEHOLIDAY.equalsIgnoreCase(holyType)) {
            mTvRestDays.setText(getString(R.string.restDays, data.getRestDays()));
          }
        }
      }
    };
  }

  private void getWorkHours(final int requestCode, String startTime, String endTime) {

    if (TextUtils.isEmpty(startTime) || TextUtils.isEmpty(endTime)) {
      return;
    }
    if (DateUtil.before(endTime, startTime)) {
      return;
    }
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_REQUISITION_TRIP_WORKHOUR);// 6.计算差旅天数.
    HashMap<String, String> params = new HashMap<>();
    String jobNumber = CurrentUser.newInstance(getActivity()).getJobNumber();
    params.put("jobno", jobNumber);// 6.计算差旅天数.
    params.put("startDate", startTime);
    params.put("endDate", endTime);
    MyUtils.showDialogLoading(getActivity(), R.string.loadingWorkHour);
    mActivity.volleyPost(url, params, new VolleyResponse() {

      @Override public void onVolleyResponse(String arg0) {

        MyUtils.dismissDialog();
        try {
          WorkHour workHour = new Gson().fromJson(arg0, WorkHour.class);
          if (workHour.getResult() == 1) {
            String days = workHour.getMessage().getDays() + "";// 6.计算差旅天数.
            String hours = workHour.getMessage().getHours() + "";

            if (requestCode == REQUEST_ACTUAL_DATE_START
                || requestCode == REQUEST_ACTUAL_DATE_END) {
              mEtActualHours.setText(hours);
              mEtActualDays.setText(days);
            }
          } else if (workHour.getResult() == 0) {
            ToastUtil.show("未找到当前人员所属考勤分组,请联系管理员核实!");
          }
        } catch (Exception e) {
          LogUtil.e("workTimeOverError=" + e, e);
          ToastUtil.show("未找到当前人员所属考勤分组,请联系管理员核实!");
        }
      }

      @Override public void onVolleyError(VolleyError arg0) {
        MyUtils.dismissDialog();
      }
    });
  }

  @Override public boolean isCorrectInput() {
    // 验证文本信息
    String reasonString = mEtReason.getText().toString().trim();
    //String currentTime = DateUtil.dateToString(new Date());// 当前时间
    String planBeginTime = mTvActualStartTime.getText().toString().trim();
    String planEndTime = mTvActualEndTime.getText().toString().trim();
    String actualBeginTime = mTvActualStartTime.getText().toString().trim();
    String actualEndTime = mTvActualEndTime.getText().toString().trim();
    String planHours = mEtActualHours.getText().toString().trim();
    if (TextUtils.isEmpty(reasonString)) {
      ToastUtil.show(R.string.no_content);
      return false;
    }
    if (TextUtils.isEmpty(mHolyType)) {
      ToastUtil.show(R.string.no_vacateType);
      return false;
    }

    if ("F".equals(ifLaterWrite)) {

      if (TextUtils.isEmpty(planBeginTime)) {
        ToastUtil.show(R.string.chooseStartTime);
        return false;
      }
      if (TextUtils.isEmpty(planEndTime)) {
        ToastUtil.show(R.string.chooseEndTime);
        return false;
      }

      if (TextUtils.isEmpty(planHours)) {
        ToastUtil.show(R.string.planNotNull);
        return false;
      }
      //			if ("0.0".equalsIgnoreCase(planHours) || "0".equalsIgnoreCase(planHours)) {
      //				ToastUtil.show(R.string.planTimeNotZero);
      //				return false;
      //			}
      if (!DateUtil.before(planBeginTime, planEndTime)) {
        ToastUtil.show(R.string.realEndTimeAfterRealStartTime);
        return false;
      }
    } else if ("T".equals(ifLaterWrite)) {

      if (TextUtils.isEmpty(actualEndTime)) {
        ToastUtil.show(R.string.chooseRealEndTime);
        return false;
      }
      if (TextUtils.isEmpty(actualBeginTime)) {
        ToastUtil.show(R.string.chooseRealStartTime);
        return false;
      }
      //			if (!TextUtils.isEmpty(actualEndTime) && DateUtil.before(currentTime, actualEndTime)) {
      //				ToastUtil.show(R.string.realEndTimeBdforeCurrent);
      //				return false;
      //			}
      //			if (!TextUtils.isEmpty(actualBeginTime) && DateUtil.before(currentTime, actualBeginTime)) {
      //				ToastUtil.show(R.string.realStartTimeBeforeCurrent);
      //				return false;
      //			}
      if (!DateUtil.before(actualBeginTime, actualEndTime)) {
        ToastUtil.show(R.string.realEndTimeAfterRealStartTime);
        return false;
      }
    }

    if (mHolyType != null && mHolyType.equalsIgnoreCase(SICK) && mRbSupplyHoly.isChecked()) {
      String attachmentStr = mFileCheck.getText().toString().trim();
      if (TextUtils.isEmpty(attachmentStr)) {
        Toast.makeText(mActivity, "病假请上传附件", Toast.LENGTH_SHORT).show();
        return false;
      }
    }
    return true;
  }

  @Override protected void volleyPostSave(final String type) {

    String url = ApiConst.URL_REQUISITION_SAVE_OR_RUN;

    RequestParams params = new RequestParams();
    params.put("attendType", CheckDetailFragment.VALUE_HOLY);
    params.put("saveOrRun", type);
    params.put("holidayKindCode", mHolyType);
    params.put("ifUserCheck", mIsNotCountType);
    params.put("overtimeReason", mEtReason.getText().toString().trim());

    /**是否补填*/
    params.put("ifLaterWrite", ifLaterWrite);

    params.put("planSTime", mTvActualStartTime.getText().toString().trim());
    params.put("planETime", mTvActualEndTime.getText().toString().trim());
    params.put("panHolyNum", mEtActualHours.getText().toString().trim());
    params.put("planHolyDays", mEtActualDays.getText().toString().trim());// 后台获取
    //如果补填
    if ("T".equals(ifLaterWrite)) {
      params.put("actSTime", mTvActualStartTime.getText().toString().trim());
      params.put("actETime", mTvActualEndTime.getText().toString().trim());
      params.put("actHolyNum", mEtActualHours.getText().toString().trim());
      params.put("actHolyDays", mEtActualDays.getText().toString().trim());
    }
    if (mMenuType == TODO) {
      //待办流程中修改请假单
      params.put("isFlow", "true");
    }
    if (mMenuType != ADD) {
      params.put("id", mId);
      if (TYPE_RUN.equals(type)) {
        params.put("isList", "0");
      }
    }
    if ("abort".equalsIgnoreCase(mDataStatus)) {
      params.put("dataStatus", "new");
    }
    if (mPath != null) {
      try {
        params.put("myfiles", new File(mPath));
      } catch (FileNotFoundException e) {
        e.printStackTrace();
        ToastUtil.show(R.string.fileNotFound);
      }
    }
      if (mMenuType != ADD) {
        params.put("accessoryName", accessoryName);
      }
    LogUtil.i("请假params===" + params.toString());
    if (TYPE_RUN.equals(type)) {

      MyUtils.showDialogLoading(getActivity(), R.string.loadingStart);
    }
    if (TYPE_SAVE.equals(type)) {

      MyUtils.showDialogLoading(getActivity(), R.string.loadingSave);
    }

    DefaultAsyncHttpClient.post(url, params, new AsyncHttpResponseHandler() {

      @Override public void onSuccess(int statusCode, Header[] headers, byte[] response) {
        MyUtils.dismissDialog();
        try {
          SuccessMessage successsMessage =
              new Gson().fromJson(new String(response), SuccessMessage.class);

          String successType = successsMessage.getResult();
          switch (successType) {
            case "1":
              getActivity().setResult(Activity.RESULT_OK);
              ToastUtil.show(R.string.success_operation);
              getActivity().finish();

              break;
            case "0":
              showErrorDialog(successsMessage.getMessage());
              break;
            default:
              ToastUtil.show(R.string.failure_operation);
              break;
          }
        } catch (Exception e) {
          ToastUtil.show(R.string.failure_operation);
        }
      }

      @Override public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
        MyUtils.dismissDialog();
        ToastUtil.show(R.string.failure_operation);
      }
    });
  }

  private void volleyPostDelete() {
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_REQUISITION_DELETE);
    HashMap<String, String> params = new HashMap<>();
    params.put("ids", mType + "#" + mId);
    MyUtils.showDialogLoading(getActivity(), R.string.loadingDelete);
    mActivity.volleyPost(url, params, new VolleyResponse() {

      @Override public void onVolleyResponse(String arg0) throws JSONException {

        MyUtils.dismissDialog();
        try {

          SuccessMessage successsMessage = new Gson().fromJson(arg0, SuccessMessage.class);

          String successType = successsMessage.getResult();

          if (successType.equals("1")) {
            getActivity().setResult(Activity.RESULT_OK);
            ToastUtil.show(R.string.success_operation);
            getActivity().finish();
          } else {
            ToastUtil.show(R.string.failure_operation);
          }
        } catch (Exception e) {
          LogUtil.e("error", e);
          ToastUtil.show(R.string.failure_operation);
        }
      }

      @Override public void onVolleyError(VolleyError arg0) {
        // Auto-generated method stub
      }
    });
  }
}
